package com.lee.common.config;

import com.lee.common.interceptor.UserInfoInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
@ConditionalOnClass(DispatcherServlet.class) // 因为网关不属于 SpringMVC 所以需要条件装配
public class MyWebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //文件磁盘图片url 映射
        // linux
//        registry.addResourceHandler("/userPhoto/**").addResourceLocations("file:/forumImage/");
//        registry.addResourceHandler("/codeimg/**").addResourceLocations("file:/root/forum1.0/forumcheckImage/");
//        registry.addResourceHandler("/mdimage/**").addResourceLocations("file:/root/forum1.0/editorImage/");

        // windows
        registry.addResourceHandler("/userPhoto/**").addResourceLocations("file:D:/forumImage/");
        registry.addResourceHandler("/codeimg/**").addResourceLocations("file:D:/forumImage/");
        registry.addResourceHandler("/mdimage/**").addResourceLocations("file:D:/forumImage/");

    }

    // 注入登录拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加登录拦截器
        registry.addInterceptor(new UserInfoInterceptor()) // 添加⽤⼾登录拦截器
                .addPathPatterns("/**") // 拦截所有请求
                .excludePathPatterns("/sign-in.html") // 排除登录HTML
                .excludePathPatterns("/sign-up.html") // 排除注册HTML
                .excludePathPatterns("/user/login") // 排除登录api接⼝
                .excludePathPatterns("/user/register") // 排除注册api接⼝
                .excludePathPatterns("/user/logout") // 排除退出api接⼝
                .excludePathPatterns("/chat.html")
                .excludePathPatterns("/swagger*/**") // 排除登录swagger下所有
                .excludePathPatterns("/v3*/**") // 排除登录v3下所有，与swag
                .excludePathPatterns("/dist/**") // 排除所有静态⽂件
                .excludePathPatterns("/image/**")
                .excludePathPatterns("/**.ico")
                .excludePathPatterns("/**.svg")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/userPhoto/**")
                .excludePathPatterns("/codeimg/**")
                .excludePathPatterns("/user/getcode");
    }
}
